package com.wiz.web.controllor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.wiz.service.FuelingService;
import com.wiz.to.FuelingTO;
import com.wiz.to.StationTO;
import com.wiz.util.DateUtil;
import com.wiz.util.ExcelUtil;
import com.wiz.vo.CollectionVO;
import com.wiz.vo.criteria.FuelingSearchCriteriaTO;

/**
 * 加油记录
 * @author xxy
 *
 */
@Controller
@RequestMapping("/fueling")
public class FuelingControllor {
	private transient final Logger log=LoggerFactory.getLogger(getClass());
	@Resource
	private FuelingService fuelingService;
	
	@RequestMapping(value="/list")
	public String getFuelingListByCriteriaTO(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "numPerPage", required = false,defaultValue="10") int pageSize,
			@RequestParam(value = "pageNum", required = false,defaultValue="1") int pageNo,
			
			@RequestParam(value = "fuellingOrderId", required = false) String fuellingOrderId,//订单id
			@RequestParam(value = "stationId", required = false) String stationId,//油站id
			@RequestParam(value = "pr", required = false) String pr,//油品
			@RequestParam(value = "memberName", required = false) String memberName,//加油客户名称
			@RequestParam(value = "groupNo", required = false) String groupNo,//班次号
			@RequestParam(value = "stationAdminName", required = false) String stationAdminName,//加油员姓名
			@RequestParam(value = "paidTypeId", required = false) Integer paidTypeId,//支付渠道:-1:无需支付,0:待确定,1:现金,2:余额支付,3:支付宝,4:微信
			@RequestParam(value = "payStatus", required = false) Integer payStatus,//支付状态:-1:无需支付,0:待确定,1:已支付
			@RequestParam(value = "beginDate", required = false) String beginDate,//日期
			@RequestParam(value = "endDate", required = false) String endDate,//日期
			HttpSession session, Model model){
		Date from = null;
		Date thru = null;
		if(null!=beginDate&&!"".equals(beginDate.trim()))
			from = DateUtil.toDate(beginDate);
		if(null!=endDate&&!"".equals(endDate.trim()))
			thru = DateUtil.toDate(endDate);
		FuelingSearchCriteriaTO fuelingSearchCriteriaTO=new FuelingSearchCriteriaTO(fuellingOrderId, stationId, pr,
				memberName, groupNo, stationAdminName,
				paidTypeId, payStatus, pageSize, pageNo, (pageNo > 0 && pageSize > 0),from,thru);
		
		List<FuelingTO> agentTOs=fuelingService.getFuelingListByCriteriaTO(fuelingSearchCriteriaTO);
		int totalCount=fuelingService.getFuelingCntByCriteriaTO(fuelingSearchCriteriaTO);
		CollectionVO collectionTO=new CollectionVO(agentTOs, totalCount, pageSize, pageNo);
		model.addAttribute("collectionTO", collectionTO);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNumShow", totalCount/pageSize+((totalCount%pageSize>0)?1:0));
		model.addAttribute("searchCriteria", fuelingSearchCriteriaTO);
		return "/fueling/list";
	}
	@RequestMapping(value="/details")
	public String details(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "fuellingOrderId", required = true) String fuellingOrderId,
			HttpSession session, Model model){
		try {
			FuelingTO fuelingTO = fuelingService.getFueling(fuellingOrderId);
			model.addAttribute("bean", fuelingTO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "fueling/details";
		
	}
	@RequestMapping(value="/dataExport")
	public String dataExport(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "numPerPage", required = false,defaultValue="10") int pageSize,
			@RequestParam(value = "pageNum", required = false,defaultValue="1") int pageNo,
			
			@RequestParam(value = "fuellingOrderId", required = false) String fuellingOrderId,//订单id
			@RequestParam(value = "stationId", required = false) String stationId,//油站id
			@RequestParam(value = "pr", required = false) String pr,//油品
			@RequestParam(value = "memberName", required = false) String memberName,//加油客户名称
			@RequestParam(value = "groupNo", required = false) String groupNo,//班次号
			@RequestParam(value = "stationAdminName", required = false) String stationAdminName,//加油员姓名
			@RequestParam(value = "paidTypeId", required = false) Integer paidTypeId,//支付渠道:-1:无需支付,0:待确定,1:现金,2:余额支付,3:支付宝,4:微信
			@RequestParam(value = "payStatus", required = false) Integer payStatus,//支付状态:-1:无需支付,0:待确定,1:已支付
			@RequestParam(value = "beginDate", required = false) Date beginDate,//日期
			@RequestParam(value = "endDate", required = false) Date endDate,//日期
			HttpSession session, Model model){
		List<List<String>>dataList = new ArrayList<List<String>>();
		FuelingTO fuelingTO = null;
		List<String> datas = null;
		Integer paidTypeIdval = null;
		Integer payStatusval = null;
		Integer calculStatus = null;
		FuelingSearchCriteriaTO fuelingSearchCriteriaTO=new FuelingSearchCriteriaTO(fuellingOrderId, stationId, pr,
				memberName, groupNo, stationAdminName,
				paidTypeId, payStatus, pageSize, pageNo, (pageNo > 0 && pageSize > 0),beginDate,endDate);
		
		List<FuelingTO> list=fuelingService.getFuelingListByCriteriaTO(fuelingSearchCriteriaTO);
		String [] headers = new String[]{
				"订单id","油站id","油枪号","提枪时间","挂枪时间",
				"油品","升数","单价","油枪累计升数","油枪累计金额",
				"加油客户id","加油客户名称","帐户车牌号","班次号",
				"员工id","加油员姓名","收银员","支付渠道","支付状态",
				"支付时间","应收金额","实收金额","即时优惠金额",
				"积分抵扣金额","授权号", "创建时间","计算状态","下放同步状态"
		};
		for(int i=0;i<list.size();i++){
			fuelingTO = list.get(i);
			paidTypeIdval = fuelingTO.getPaidTypeId();
			payStatusval = fuelingTO.getPayStatus();
			calculStatus = fuelingTO.getCalculStatus();
			
			datas = new ArrayList<String>();
			datas.add(fuelingTO.getFuellingOrderId());//订单id
			datas.add(fuelingTO.getStationId());//油站id
			datas.add(fuelingTO.getFpId()+"");//油枪号
			datas.add(DateUtil.formatDate(fuelingTO.getFpStartTime()));//提枪时间
			datas.add(DateUtil.formatDate(fuelingTO.getFpEndTime()));//挂枪时间
			datas.add(fuelingTO.getPr());//油品
			datas.add(fuelingTO.getVol()+"");//升数
			datas.add(fuelingTO.getPrice()+"");//单价
			datas.add(fuelingTO.getTotalVol()+"");//油枪累计升数
			datas.add(fuelingTO.getTotalAmount()+"");//油枪累计金额
			datas.add(fuelingTO.getMemberId());//加油客户id
			datas.add(fuelingTO.getMemberName());//加油客户名称
			datas.add(fuelingTO.getMemberLicensePlate());//帐户车牌号
			datas.add(fuelingTO.getGroupNo());//班次号
			datas.add(fuelingTO.getStationAdminId());//员工id
			datas.add(fuelingTO.getStationAdminName());//加油员姓名
			datas.add(fuelingTO.getStationCashier());//收银员
			switch (paidTypeIdval) {
			case -1:
				datas.add("无需支付");
				break;
			case 0:
				datas.add("待确定");
				break;
			case 1:
				datas.add("现金");
				break;
			case 2:
				datas.add("余额支付");
				break;
			case 3:
				datas.add("支付宝");
				break;
			case 4:
				datas.add("微信");
				break;

			default:
				datas.add("");
				break;
			}//支付渠道
			switch (payStatusval) {
			case -1:
				datas.add("无需支付");
				break;
			case 0:
				datas.add("待确定");
				break;
			case 1:
				datas.add("已支付");
				break;
			default:
				datas.add("");
				break;
			}//支付状态
			datas.add(DateUtil.formatDate(fuelingTO.getPayTime()));//支付时间=客户端上传时间
			datas.add(fuelingTO.getReceivableAmount()+"");//应收金额
			datas.add(fuelingTO.getRealincomeAmount()+"");//实收金额
			datas.add(fuelingTO.getDiscountAmount()+"");//即时优惠金额
			datas.add(fuelingTO.getIntegralAmount()+"");//积分抵扣金额
			datas.add(fuelingTO.getAuthorityId());//授权号
			datas.add(DateUtil.formatDate(fuelingTO.getCreatedTime()));//创建时间=客户端上传时间
			switch (calculStatus) {
			case 0:
				datas.add("未计算");
				break;
			case 1:
				datas.add("已计算");
				break;
			
			default:
				datas.add("");
				break;
			}//计算状态，
			datas.add(fuelingTO.getDwonloadSyncStatus());//下放同步状态
			dataList.add(datas);
		}
        try {
        	// 设置response参数，可以打开下载页面
        	response.reset();
        	response.setContentType("application/vnd.ms-excel;charset=utf-8");
        	response.setHeader("Content-Disposition", "attachment;filename=" + new String(( "加油数据.xls").getBytes(), "iso-8859-1"));
            ExcelUtil.export("加油数据",  Arrays.asList(headers), dataList, response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

		return null;
		
	}
}
